---
description: 'Enforce selecting specific fields when they are available on the GraphQL type.'
---

# `require-selections`

✅ The `"extends": "plugin:@graphql-eslint/operations-recommended"` property in a configuration file
enables this rule.

💡 This rule provides
[suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)

- Category: `Operations`
- Rule name: `@graphql-eslint/require-selections`
- Requires GraphQL Schema: `true`
  [ℹ️](/docs/getting-started#extended-linting-rules-with-graphql-schema)
- Requires GraphQL Operations: `true`
  [ℹ️](/docs/getting-started#extended-linting-rules-with-siblings-operations)

{metadata.description}

## Usage Examples

### Incorrect

```graphql
# eslint @graphql-eslint/require-selections: 'error'

# In your schema
type User {
  id: ID!
  name: String!
}

# Query
query {
  user {
    name
  }
}
```

### Correct

```graphql
# eslint @graphql-eslint/require-selections: 'error'

# In your schema
type User {
  id: ID!
  name: String!
}

# Query
query {
  user {
    id
    name
  }
}

# Selecting `id` with an alias is also valid
query {
  user {
    id: name
  }
}
```

## Config Schema

The schema defines the following properties:

### `fieldName`

The object must be one of the following types:

- `asString`
- `asArray`

Default: `"id"`

### `requireAllFields` (boolean)

Whether all fields of `fieldName` option must be included.

---

# Sub Schemas

The schema defines the following additional types:

## `asString` (string)

## `asArray` (array)

## When Not To Use It

Relay Compiler automatically adds an `id` field to any type that has an `id` field, even if it
hasn't been explicitly requested. Requesting a field that is not used directly in the code can
conflict with another Relay rule: `relay/unused-fields`.

## Resources

- [Rule source](https://github.com/dimaMachina/graphql-eslint/tree/master/packages/plugin/src/rules/require-selections.ts)
- [Test source](https://github.com/dimaMachina/graphql-eslint/tree/master/packages/plugin/__tests__/require-selections.spec.ts)
